/*
 EasySinhala. An English to Sinhala translitratior.
 Copyright (C) 2009  S. Suhothayan

 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.

 this is a product by S.Suhothayan
 CSE
 University of Moratuwa
 */

EasySinhala.ns(function() {
    with (EasySinhala.LIB) {

        theApp.ModuleES =
        {
            totalText:'',
            tempText:'',
            text:'',
            space:true,
            Sinhala_on:false,        //shows whether easysinhala is enabled or not
            focusedElement:'',        // will have the currently focused element
            ctrlKey:false,
            altKey:false,
            tooltipTextDisplayLength:30,
            re1 : /\s[^ ]+\s$/g,                // to get the last word
            re2 : /\s[^ ]+$/g ,
            re3 : /^[^ ]*\s$/g,
            re4 : /^[^ ]*$/g,

            dump: function(aMessage) {
                var consoleService = Components.classes["@mozilla.org/consoleservice;1"]
                        .getService(Components.interfaces.nsIConsoleService);
                consoleService.logStringMessage("EasySinhala: " + aMessage);
            },
            init: function() {        //this will enable and disable easysinhala
                // addEventListener("click", this.hidePanelfunc, true);
                if (this.Sinhala_on == false) {    //enabling
                    addEventListener("focus", this.focusedElementForTooltip, true);
                    addEventListener("keydown", this.TextStop, true);
                    addEventListener("keyup", this.focusedElementForTextReplace, true);
                    addEventListener("click", this.focusedElementForTooltipClick, true);
                    addEventListener("blur", this.hidetooltipfunc, true);

                    this.Sinhala_on = true;
                } else {                    //desabling
                    this.hidetooltipfunc();
                    removeEventListener("focus", this.focusedElementForTooltip, true);
                    removeEventListener("keydown", this.TextStop, true);
                    removeEventListener("keyup", this.focusedElementForTextReplace, true);
                    removeEventListener("click", this.focusedElementForTooltipClick, true);
                    // removeEventListener("click", this.hidePanelfunc, true);
                    removeEventListener("blur", this.hidetooltipfunc, true);

                    this.Sinhala_on = false;
                }
                document.getElementById("easysinhalaButton").setAttribute("state", (this.Sinhala_on ? "on" : "off"));			//will show the state of easysinhala at the UI
                document.getElementById("easySinhala-enabled").setAttribute("checked", (this.Sinhala_on ? "true" : "false"));
                document.getElementById("easySinhala-enabled1").setAttribute("checked", (this.Sinhala_on ? "true" : "false"));
                document.getElementById("easySinhala-enabled2").setAttribute("checked", (this.Sinhala_on ? "true" : "false"));
                document.getElementById("easysinhalaPanelIcon").setAttribute("state", (this.Sinhala_on ? "on" : "off"));
                document.getElementById("easysinhalaStatusbarIcon").setAttribute("state", (this.Sinhala_on ? "on" : "off"));

            },

            // to show the tool tip with the transliterated sinhala world///////////////////////////////
            focusedElementForTooltipClick: function(e) {
                EasySinhala.ModuleES.focusedElementForTooltip(e);
            },

            focusedElementForTooltip: function(e) {

                //this.focusedElement = document.commandDispatcher.focusedElement;

                this.focusedElement = e.target;

                if (this.focusedElement != null) {

                    var name;
                    if (this.focusedElement.localName)
                        name = this.focusedElement.localName.toUpperCase();//this line of code nores the password fields selections to be shown in the preview
                    var type = this.focusedElement.type;
                    //  this.dump(name);//todo
                    if (name == "TEXTAREA" || name == "TEXTBOX" || name == "SEARCHBAR" || (name == "INPUT" && (type == "text" || type == "file")))//to avoid showing password fields content translated
                    {

                        var tooltip = document.getElementById("easySinhalaTooltip");
                        tooltip.openPopup(this.focusedElement, "before_start", 0, 0, false, false);
                        var tooltipElement = document.getElementById("tooltipValue");
                        tooltipElement.value = EasySinhala.ModuleES.showText(this.focusedElement, name);
                        //  this.dump(tooltipElement.value);
                    }
                }
            }
            ,

            showText: function(objElement, name) {        // to displaying the sample text _ tanslitriated

                if (this.Sinhala_on == true) {
                    var cursorPosition = EasySinhala.ModuleES.doGetCaretPosition(objElement);
                    //  this.dump("cp:" + cursorPosition);
                    this.totalText = objElement.value;
                    var baforeCurser = this.totalText.substring(0, cursorPosition);
                    if (name == "SEARCHBAR") {

                        baforeCurser = this.totalText;
                        this.text = this.totalText;

                    }

                    this.tempText = baforeCurser.match(this.re1);
                    this.space = true;
                    if (this.tempText == null) {
                        this.tempText = baforeCurser.match(this.re2);
                        this.space = false;
                        if (this.tempText == null) {
                            this.tempText = baforeCurser.match(this.re3);
                            this.space = true;
                            if (this.tempText == null) {
                                this.tempText = baforeCurser.match(this.re4);
                                this.space = false;
                                if (this.tempText == null) {
                                    this.space = false;
                                    this.tempText = "";
                                }
                            }
                        }
                    }
                    this.text = this.tempText.toString();


                    this.text = EasySinhala.ModuleES.translate(this.text);							// translitriating
                    if (this.space == true) {
                        return "";
                    } else {
                        if (this.text.length > this.tooltipTextDisplayLength) {

                            return "..." + this.text.substring(this.text.length - this.tooltipTextDisplayLength, this.text.length);
                        } else {
                            return this.text;
                        }
                    }
                } else
                    return "";
            }
            ,

            hidetooltipfunc: function() {    //hiding tooltip
                var tooltipElemant = document.getElementById("easySinhalaTooltip");
                tooltipElemant.hidePopup();
            }
            ,
            //end of showing tooltips

            //to replace the english words with the sinhala ones
            focusedElementForTextReplace: function(e) {        // gets the text fields
                var unicode = e.keyCode ? e.keyCode : e.charCode
                //var this.focusedElement = document.commandDispatcher.focusedElement;
                focusedElement = e.target;
                if (focusedElement != null) {
                    if (focusedElement.localName)
                        name = focusedElement.localName.toUpperCase();//this line of code igonores the password fields selections to be shown in the preview
                    var type = focusedElement.type;
                    if (name == "TEXTAREA" || name == "TEXTBOX" || name == "SEARCHBAR" || (name == "INPUT" && (type == "text" || type == "file")))//to avoid showing password fields content translated
                    {
                        var tooltipElemant = document.getElementById("easySinhalaTooltip");
                        tooltipElemant.openPopup(focusedElement, "before_start", 0, 0, false, false);
                        var tooltipElemantValue = document.getElementById("tooltipValue");
                        EasySinhala.ModuleES.cahangeText(e, focusedElement, tooltipElemantValue, name);
                    } else if (name != "INPUT") {
                        EasySinhala.ModuleES.hidetooltipfunc();
                        if ("48" <= unicode && unicode <= "57" || "65" <= unicode && unicode <= "90" || unicode == "32") {// enabling only for selected keys
                            if (!(this.altKey || this.ctrlKey)) {
                                if (e && e.preventDefault)             //prevent default action by keyboard
                                    e.preventDefault();
                                EasySinhala.ModuleES.showPenal(e, "false");
                            }
                        }
                    } else
                        this.hidetooltipfunc();
                }
                if (unicode == "17")     // control key
                    this.ctrlKey = false;
                if (unicode == "18")      // alt key
                    this.altKey = false;
            }
            ,

            cahangeText: function(e, objElement, tooltipObj, name) {    // to transliterate the word//not yet set
                if (this.Sinhala_on == true) {
                    this.space = false;
                    var unicode = e.keyCode ? e.keyCode : e.charCode
                    var enter = false;
                    var show = false;
                    if (unicode == "27") {
                        this.hidePanel();
                        this.hidetooltipfunc();
                    } else {
                        if (unicode == "32") {  // @ space
                            this.space = true;
                        } else if (unicode == "13") {
                            this.space = true;
                            enter = true;
                        } else if (unicode == "37" || unicode == "38" || unicode == "39" || unicode == "40") {    // not to transliterate when arrow keys are pressed
                            this.space = false;
                            show = true;
                        } else {
                            this.space = false;
                        }
                        var cursorPosition = EasySinhala.ModuleES.doGetCaretPosition(objElement);
                        this.totalText = objElement.value;
                        var baforeCurser = this.totalText.substring(0, cursorPosition);
                        var afterCurser = this.totalText.substring(cursorPosition);
                        // alert(cursorPosition);
                        if (name == "SEARCHBAR") {
                            baforeCurser = this.totalText;
                            afterCurser = "";
                            // this.text = EasySinhala.ModuleES.translate(this.totalText);
                            // objElement.value =  this.text ;
                            // tooltipObj.value=   this.text ;
                        }

                        this.tempText = baforeCurser.match(this.re1);
                        var space1 = true;
                        if (this.tempText == null) {
                            this.tempText = baforeCurser.match(this.re2);
                            space1 = false;
                            if (this.tempText == null) {
                                this.tempText = baforeCurser.match(this.re3);
                                space1 = true;
                                if (this.tempText == null) {
                                    this.tempText = baforeCurser.match(this.re4);
                                    space1 = false;
                                    if (this.tempText == null) {
                                        space1 = false;
                                        this.tempText = "";
                                    }
                                }
                            }
                        }
                        this.text = this.tempText.toString();
                        baforeCurser = baforeCurser.substring(0, baforeCurser.length - this.text.length);
                        this.text = EasySinhala.ModuleES.translate(this.text);

                        var baforeFinalCurser;
                        if (enter == true) {                            // setting all back after tracslitration
                            baforeFinalCurser = baforeCurser + this.text + "\u0085";
                        }
                        else {
                            baforeFinalCurser = baforeCurser + this.text;
                        }

                    }
                    if (show == true && space1 == true) {
                        tooltipObj.value = "";
                    } else {
                        if (this.space == true) {
                            objElement.value = baforeFinalCurser + afterCurser;
                            tooltipObj.value = "";
                            EasySinhala.ModuleES.setCaretPosition(objElement, baforeFinalCurser.length);
                        } else {
                            if (this.text.length > this.tooltipTextDisplayLength) {

                                tooltipObj.value = "..." + this.text.substring(this.text.length - this.tooltipTextDisplayLength, this.text.length);
                            } else {
                                tooltipObj.value = this.text;
                            }

                        }
                    }
                    //                    }
                    //                    }
                } else
                    tooltipObj.value = "";
            }
            ,
            //end of replacing the words

            //showing panels to not defiend fields

            TextStop: function(e) {        //to prevent default action by keyboard

                var unicode = e.keyCode ? e.keyCode : e.charCode
                if (unicode == "17")
                    this.ctrlKey = true;
                if (unicode == "18")
                    this.altKey = true;
                //var focusedElement = document.commandDispatcher.focusedElement;
                var focusedElement = e.target;
                while (focusedElement != null && focusedElement.nodeType != focusedElement.ELEMENT_NODE)
                    focusedElement = focusedElement.parentNode;
                if (focusedElement != null) {
                    if (focusedElement.localName)
                        name = focusedElement.localName.toUpperCase();//this line of code igonores the password fields selections to be shown in the preview

                    if (name == "TEXTAREA" || name == "TEXTBOX" || name == "SEARCHBAR" || name == "INPUT")//to avoid showing password fields content translated
                    {

                    } else {
                        EasySinhala.ModuleES.hidetooltipfunc();

                        if ("48" <= unicode && unicode <= "57" || "65" <= unicode && unicode <= "90" || unicode == "32") {// enabling only for selected keys
                            if (!(this.altKey || this.ctrlKey)) {
                                if (e && e.preventDefault)             //prevent default action by keyboard
                                    e.preventDefault();
                                EasySinhala.ModuleES.showPenal(e, "false");
                            }
                        }
                    }
                }
            }
            ,


            showPenal: function(e, str) {
                var panelfocusedElement = document.commandDispatcher.focusedElement;
                var x = 400;		//positon
                var y = 500;
                if (panelfocusedElement != null && str == "false") {
                    if (panelfocusedElement.tagName != "HTML") {     //not iframe
                        x = 0;
                        y = 0;
                    }
                }
                EasySinhala.ModuleES.hidetooltipfunc();
                var unicode = e.keyCode ? e.keyCode : e.charCode;
                if (("48" <= unicode && unicode <= "57" || "65" <= unicode && unicode <= "90" || unicode == "32" || unicode == "17" || unicode == "18") || str == "true") {// enabling only for selected keys
                    var tempTextanel = document.getElementById("easySinhalaPanel");
                    tempTextanel.openPopup(panelfocusedElement, "after_pointer", x, y, false, false);
                    var textboxPenel = document.getElementById("eSpopuptextbox");
                    //tempTextanel.focus();
                    textboxPenel.focus();
                    //textboxPenel.select();
                } else {
                    this.hidePanel();
                }
            }
            ,

            copyit: function() {                // to copy the text in the text area of the panel
                var penel = document.getElementById("eSpopuptextbox");
                penel.focus();
                textToCopy = penel.value;
                const gClipboardHelper = Components.classes["@mozilla.org/widget/clipboardhelper;1"].
                        getService(Components.interfaces.nsIClipboardHelper);
                gClipboardHelper.copyString(textToCopy);
                EasySinhala.ModuleES.cleartext();
            }
            ,

            cleartext: function () {            // to clear the text in the text area of the panel
                var penel = document.getElementById("eSpopuptextbox");
                penel.value = "";
                this.hidePanel();
                this.hidetooltipfunc();
            }
            ,

            hidePanel: function () {        //hiding the panel
                //var focusedElement = document.commandDispatcher.focusedElement;
                var penel = document.getElementById("easySinhalaPanel");
                penel.hidePopup();
            }
            ,


            //end of showing panel/////////////////////////////////////////////////////////////

            // getting and setting Chareter Positions/////////////////////////////////////////////////

            setCaretPosition: function (focusedEliment, pos) { // setting the curser position within the text field
                // after trasliterating

                if (focusedEliment.setSelectionRange) {
                    focusedEliment.focus();
                    focusedEliment.setSelectionRange(pos, pos);
                }
                else if (focusedEliment.createTextRange) {
                    var range = focusedEliment.createTextRange();
                    range.collapse(true);
                    range.moveEnd('character', pos);
                    range.moveStart('character', pos);
                    range.select();
                }
            }
            ,

            doGetCaretPosition: function  (focusedElimentToGetPosi) {// getting the curser position within the text field
                var CaretPos = 0;
                // alert(focusedElimentToGetPosi+focusedElimentToGetPosi.localName+focusedElimentToGetPosi.type+focusedElimentToGetPosi);
                if (focusedElimentToGetPosi.selectionStart || focusedElimentToGetPosi.selectionStart == '0')
                    CaretPos = focusedElimentToGetPosi.selectionStart;
                return (CaretPos);
            }
            ,

            //end of getting and setting Chareter Positions/////////////////////////////////////////////////////

            //other  supporting feature functions////////////////////////////////////////////////////////////

            openURL: function(url) {    //to open the url on a new page
                if (window.delayedOpenTab)
                    window.delayedOpenTab(url)
                else
                    Components.classes["@mozilla.org/uriloader/external-protocol-service;1"]
                            .getService(Components.interfaces.nsIExternalProtocolService)
                            .loadUrl(this.url2uri(url));
            }            ,

            clickButten: function (e) {    //to check wheter ts right click or left
                var rightclick;
                if (!e)
                    var e = window.event;
                if (e.which)
                    rightclick = (e.which == 3);
                else if (e.button)
                    rightclick = (e.button == 2);
                if (rightclick == false)        //if left click toggle the state of easysinhala
                    EasySinhala.ModuleES.init();
            },

            //end of other  suporting feature functions////////////////////////////////////////////////////////////
            translate: function(text) {        // traslierating function
                //var text;
                var nVowels;
                var consonants = new Array();
                var consonantsUni = new Array();
                var vowels = new Array();
                var vowelsUni = new Array();
                var vowelModifiersUni = new Array();
                var specialConsonants = new Array();
                var specialConsonantsUni = new Array();
                var specialCharUni = new Array();
                var specialChar = new Array();


                vowelsUni[0] = 'ඌ';
                vowels[0] = 'oo';
                vowelModifiersUni[0] = 'ූ';
                vowelsUni[1] = 'ඕ';
                vowels[1] = 'o\\)';
                vowelModifiersUni[1] = 'ෝ';
                vowelsUni[2] = 'ඕ';
                vowels[2] = 'oe';
                vowelModifiersUni[2] = 'ෝ';
                vowelsUni[3] = 'ආ';
                vowels[3] = 'aa';
                vowelModifiersUni[3] = 'ා';
                vowelsUni[4] = 'ආ';
                vowels[4] = 'a\\)';
                vowelModifiersUni[4] = 'ා';
                vowelsUni[5] = 'ඈ';
                vowels[5] = 'Aa';
                vowelModifiersUni[5] = 'ෑ';
                vowelsUni[6] = 'ඈ';
                vowels[6] = 'A\\)';
                vowelModifiersUni[6] = 'ෑ';
                vowelsUni[7] = 'ඈ';
                vowels[7] = 'ae';
                vowelModifiersUni[7] = 'ෑ';
                vowelsUni[8] = 'ඊ';
                vowels[8] = 'ii';
                vowelModifiersUni[8] = 'ී';
                vowelsUni[9] = 'ඊ';
                vowels[9] = 'i\\)';
                vowelModifiersUni[9] = 'ී';
                vowelsUni[10] = 'ඊ';
                vowels[10] = 'ie';
                vowelModifiersUni[10] = 'ී';
                vowelsUni[11] = 'ඊ';
                vowels[11] = 'ee';
                vowelModifiersUni[11] = 'ී';
                vowelsUni[12] = 'ඒ';
                vowels[12] = 'ea';
                vowelModifiersUni[12] = 'ේ';
                vowelsUni[13] = 'ඒ';
                vowels[13] = 'e\\)';
                vowelModifiersUni[13] = 'ේ';
                vowelsUni[14] = 'ඒ';
                vowels[14] = 'ei';
                vowelModifiersUni[14] = 'ේ';
                vowelsUni[15] = 'ඌ';
                vowels[15] = 'uu';
                vowelModifiersUni[15] = 'ූ';
                vowelsUni[16] = 'ඌ';
                vowels[16] = 'u\\)';
                vowelModifiersUni[16] = 'ූ';
                vowelsUni[17] = 'ඖ';
                vowels[17] = 'au';
                vowelModifiersUni[17] = 'ෞ';
                vowelsUni[18] = 'ඇ';
                vowels[18] = '\\\\a';
                vowelModifiersUni[18] = 'ැ';

                vowelsUni[19] = 'අ';
                vowels[19] = 'a';
                vowelModifiersUni[19] = '';
                vowelsUni[20] = 'ඇ';
                vowels[20] = 'A';
                vowelModifiersUni[20] = 'ැ';
                vowelsUni[21] = 'ඉ';
                vowels[21] = 'i';
                vowelModifiersUni[21] = 'ි';
                vowelsUni[22] = 'එ';
                vowels[22] = 'e';
                vowelModifiersUni[22] = 'ෙ';
                vowelsUni[23] = 'උ';
                vowels[23] = 'u';
                vowelModifiersUni[23] = 'ු';
                vowelsUni[24] = 'ඔ';
                vowels[24] = 'o';
                vowelModifiersUni[24] = 'ො';
                vowelsUni[25] = 'ඓ';
                vowels[25] = 'I';
                vowelModifiersUni[25] = 'ෛ';
                nVowels = 26;

                specialConsonantsUni[0] = 'ං';
                specialConsonants[0] = /\\n/g;
                specialConsonantsUni[1] = 'ඃ';
                specialConsonants[1] = /\\h/g;
                specialConsonantsUni[2] = 'ඞ';
                specialConsonants[2] = /\\N/g;
                specialConsonantsUni[3] = 'ඍ';
                specialConsonants[3] = /\\R/g;
                //special characher Repaya
                specialConsonantsUni[4] = 'ර්' + '\u200D';
                specialConsonants[4] = /R/g;
                specialConsonantsUni[5] = 'ර්' + '\u200D';
                specialConsonants[5] = /\\r/g;

                consonantsUni[0] = 'ඬ';
                consonants[0] = 'nnd';
                consonantsUni[1] = 'ඳ';
                consonants[1] = 'nndh';
                consonantsUni[2] = 'ඟ';
                consonants[2] = 'nng';
                consonantsUni[3] = 'ථ';
                consonants[3] = 'th';
                consonantsUni[4] = 'ධ';
                consonants[4] = 'dh';
                consonantsUni[5] = 'ඝ';
                consonants[5] = 'gh';
                consonantsUni[6] = 'ඡ';
                consonants[6] = 'ch';
                consonantsUni[7] = 'ඵ';
                consonants[7] = 'ph';
                consonantsUni[8] = 'භ';
                consonants[8] = 'bh';
                consonantsUni[9] = 'ඣ';
                consonants[9] = 'jh';
                consonantsUni[10] = 'ෂ';
                consonants[10] = 'sh';
                consonantsUni[11] = 'ඥ';
                consonants[11] = 'GN';
                consonantsUni[12] = 'ඤ';
                consonants[12] = 'KN';
                //consonantsUni[13] = 'ළු';
                //consonants[13] = 'Lu';
                consonantsUni[14] = 'ඛ';
                consonants[14] = 'kh';
                consonantsUni[15] = 'ඨ';
                consonants[15] = 'Th';
                consonantsUni[16] = 'ඪ';
                consonants[16] = 'Dh';

                consonantsUni[17] = 'ශ';
                consonants[17] = 'S';
                consonantsUni[18] = 'ද';
                consonants[18] = 'd';
                consonantsUni[19] = 'ච';
                consonants[19] = 'c';
                consonantsUni[20] = 'ත';
                consonants[20] = 't';
                consonantsUni[21] = 'ට';
                consonants[21] = 'T';
                consonantsUni[22] = 'ක';
                consonants[22] = 'k';
                consonantsUni[23] = 'ඩ';
                consonants[23] = 'D';
                consonantsUni[24] = 'න';
                consonants[24] = 'n';
                consonantsUni[25] = 'ප';
                consonants[25] = 'p';
                consonantsUni[26] = 'බ';
                consonants[26] = 'b';
                consonantsUni[27] = 'ම';
                consonants[27] = 'm';
                consonantsUni[28] = '‍ය';
                consonants[28] = '\\u005C' + 'y';
                consonantsUni[29] = '‍ය';
                consonants[29] = 'Y';
                consonantsUni[30] = 'ය';
                consonants[30] = 'y';
                consonantsUni[31] = 'ජ';
                consonants[31] = 'j';
                consonantsUni[32] = 'ල';
                consonants[32] = 'l';
                consonantsUni[33] = 'ව';
                consonants[33] = 'v';
                consonantsUni[34] = 'ව';
                consonants[34] = 'w';
                consonantsUni[35] = 'ස';
                consonants[35] = 's';
                consonantsUni[36] = 'හ';
                consonants[36] = 'h';
                consonantsUni[37] = 'ණ';
                consonants[37] = 'N';
                consonantsUni[38] = 'ළ';
                consonants[38] = 'L';
                consonantsUni[39] = 'ඛ';
                consonants[39] = 'K';
                consonantsUni[40] = 'ඝ';
                consonants[40] = 'G';
                consonantsUni[41] = 'ඵ';
                consonants[41] = 'P';
                consonantsUni[42] = 'ඹ';
                consonants[42] = 'B';
                consonantsUni[43] = 'ෆ';
                consonants[43] = 'f';
                consonantsUni[44] = 'ග';
                consonants[44] = 'g';
                //last because we need to ommit this in dealing with Rakaransha
                consonantsUni[45] = 'ර';
                consonants[45] = 'r';

                specialCharUni[0] = 'ෲ';
                specialChar[0] = 'ruu';
                specialCharUni[1] = 'ෘ';
                specialChar[1] = 'ru';
                //specialCharUni[2]='්‍ර'; specialChar[2]='ra';

                var s,r,v;
                // text = document.txtBox.box1.value;

                //special consonents
                for (var i = 0; i < specialConsonants.length; i++)
                    text = text.replace(specialConsonants[i], specialConsonantsUni[i]);

                //consonents + special Chars
                for (var i = 0; i < specialCharUni.length; i++) {
                    for (var j = 0; j < consonants.length; j++) {
                        s = consonants[j] + specialChar[i];
                        v = consonantsUni[j] + specialCharUni[i];
                        r = new RegExp(s, "g");
                        text = text.replace(r, v);
                    }
                }
                //consonants + Rakaransha + vowel modifiers
                for (var j = 0; j < consonants.length; j++) {
                    for (var i = 0; i < vowels.length; i++) {
                        s = consonants[j] + "r" + vowels[i];
                        v = consonantsUni[j] + "්‍ර" + vowelModifiersUni[i];
                        r = new RegExp(s, "g");
                        text = text.replace(r, v);
                    }
                    s = consonants[j] + "r";
                    v = consonantsUni[j] + "්‍ර";
                    r = new RegExp(s, "g");
                    text = text.replace(r, v);
                }
                //consonents + vowel modifiers
                for (var i = 0; i < consonants.length; i++) {
                    for (var j = 0; j < nVowels; j++) {
                        s = consonants[i] + vowels[j];
                        v = consonantsUni[i] + vowelModifiersUni[j];
                        r = new RegExp(s, "g");
                        text = text.replace(r, v);
                    }
                }

                //consonents + HAL
                for (var i = 0; i < consonants.length; i++) {
                    r = new RegExp(consonants[i], "g");
                    text = text.replace(r, consonantsUni[i] + "්");
                }

                //vowels
                for (var i = 0; i < vowels.length; i++) {
                    r = new RegExp(vowels[i], "g");
                    text = text.replace(r, vowelsUni[i]);
                }


                return text;
            }
        }

    }
});
